/** **************************************************
 * Description: 向hql中加入参数的类
 * Copyright:   Copyright (c) 2008
 * Company:    cl
 * @author     caoxl
 * @version    1.0
 * @see
 HISTORY
 *  2008-7-1 caoxl Create File
 ************************************************* */
package com.bfsuol.bfsuolframework.core.dao.support;

import org.apache.log4j.Logger;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;


public class HqlParameter {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(HqlParameter.class);
	
	
	public static final int STRING = 1;
	public static final int BOOLEAN = 2;
	public static final int INT = 3;
	public static final int LONG = 4;
	public static final int DATE = 5;
	public static final int FLOAT = 6;
	public static final int DOUBLE = 7;

	private Set<Parameter> parameters = new HashSet<Parameter>();



	public static HqlParameter newInstance(){
		return new HqlParameter();
	}
	public static HqlParameter newInstance(String key, String value){
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, boolean value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, Boolean value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, int value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, Integer value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, long value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, Long value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, Date value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, float value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, Float value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, double value) {
		return new HqlParameter(key, value);
	}
	public static HqlParameter newInstance(String key, Double value) {
		return new HqlParameter(key, value);
	}

	public Collection<Parameter> getParameters(){
		return this.parameters;
	}
	public boolean isEmpty(){
		return parameters==null || parameters.isEmpty();
	}

	private HqlParameter(){}
	private HqlParameter(String key,String value){
		addParameter(key, value);
	}
	private HqlParameter(String key, boolean value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, Boolean value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, int value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, Integer value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, long value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, Long value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, Date value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, float value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, Float value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, double value) {
		addParameter(key, value);
	}
	private HqlParameter(String key, Double value) {
		addParameter(key, value);
	}

	/*
	 * 设置
	 */

	public HqlParameter addParameter(String key, String value){
		addParameter(key, value,STRING);
		return this;
	}
	public HqlParameter addParameter(String key, boolean value) {
		addParameter(key, value,BOOLEAN);
		return this;
	}
	public HqlParameter addParameter(String key, Boolean value) {
		addParameter(key, value,BOOLEAN);
		return this;
	}
	public HqlParameter addParameter(String key, int value) {
		addParameter(key, value,INT);
		return this;
	}
	public HqlParameter addParameter(String key, Integer value) {
		addParameter(key, value,INT);
		return this;
	}
	public HqlParameter addParameter(String key, long value) {
		addParameter(key, value,LONG);
		return this;
	}
	public HqlParameter addParameter(String key, Long value) {
		addParameter(key, value,LONG);
		return this;
	}
	public HqlParameter addParameter(String key, Date value) {
		addParameter(key, value,DATE);
		return this;
	}
	public HqlParameter addParameter(String key, float value) {
		addParameter(key, value,FLOAT);
		return this;
	}
	public HqlParameter addParameter(String key, Float value) {
		addParameter(key, value,FLOAT);
		return this;
	}
	public HqlParameter addParameter(String key, double value) {
		addParameter(key, value,DOUBLE);
		return this;
	}
	public HqlParameter addParameter(String key, Double value) {
		addParameter(key, value,DOUBLE);
		return this;
	}

	private void addParameter(String key,Object value,int type){
		if(logger.isDebugEnabled()){
			logger.debug("add HqlParameter key = "+key+" value = " + value + " type = " + type);
		}
		parameters.add(new Parameter(key,value,type));
	}
	
	public class Parameter{
		/**
		 * Logger for this class
		 */
		private final Logger logger = Logger.getLogger(Parameter.class);
		
		private int type;
		private String key;
		private Object value;
		
		
		public Parameter( String key, Object value, int type) {
			this.type = type;
			this.key = key;
			this.value = value;
		}
		public String getKey() {
			return key;
		}
		public void setKey(String key) {
			this.key = key;
		}
		public int getType() {
			return type;
		}
		public void setType(int type) {
			this.type = type;
		}
		public Object getValue() {
			return value;
		}
		public void setValue(Object value) {
			this.value = value;
		}
		
		/*
		 * 获得
		 */
		public String getStringValue() {
			return (String)value;
		}
		public int getIntValue() {
			return (Integer)value;
		}
		public long getLongValue() {
			if(logger.isDebugEnabled()){
				logger.debug("get Long value" + value);
			}
			return (Long)value;
		}
		public boolean getBooleanValue() {
			return (Boolean)value;
		}
		public float getFloatValue() {
			return (Float)value;
		}
		public double getDoubleValue() {
			return (Double)value;
		}
		public Date getDateValue() {
			return (Date)value;
		}
		
		@Override
		public int hashCode() {
			final int PRIME = 31;
			int result = 1;
			result = PRIME * result + ((key == null) ? 0 : key.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			final Parameter other = (Parameter) obj;
			if (key == null) {
				if (other.key != null)
					return false;
			} else if (!key.equals(other.key)){
				return false;
			}
			return true;
		}
		
		
	}
}
